<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Payment_model extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	/**
	 * 결제 내역 리스트 가져오기
	 */
	function select_payment_history_list($ptype, $user_id, $user_name, $start_dt, $end_dt, $page_no, $page_size) {
		log_message('debug',"#### Payment_model::select_payment_history_list($ptype, $user_id, $user_name, $start_dt, $end_dt, $page_no, $page_size)");

		$start_row = ($page_no-1) * $page_size;

		if (!empty($ptype)) {
			$this->db->where('ptype', $ptype);
		}
		if (!empty($user_id)) {
			$this->db->where('user_id', $user_id);
		}
		if (!empty($user_name)) {
			$this->db->where('ordnm', $user_name);
		}
		if (!empty($start_dt) && !empty($end_dt)) {
			$this->db->where('SUBSTRING(create_dt,1,10) BETWEEN ', " '$start_dt' AND '$end_dt' ", FALSE);
		}
		$this->db->order_by('idx', 'desc');
		$this->db->limit($page_size, $start_row);
		$query = $this->db->get('tb_payment_history') or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 총 내역 수
	 */
	function get_payment_history_total_count($ptype, $user_id, $user_name, $start_dt, $end_dt) {
		log_message('debug',"#### Payment_model::get_payment_history_total_count($ptype, $user_id, $user_name, $start_dt, $end_dt)");

		if (!empty($ptype)) {
			$this->db->where('ptype', $ptype);
		}
		if (!empty($user_id)) {
			$this->db->where('user_id', $user_id);
		}
		if (!empty($user_name)) {
			$this->db->where('ordnm', $user_name);
		}
		if (!empty($start_dt) && !empty($end_dt)) {
			$this->db->where('SUBSTRING(create_dt,1,10) BETWEEN ', " '$start_dt' AND '$end_dt' ", FALSE);
		}
		$this->db->from('tb_payment_history');
		$result = $this->db->count_all_results();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
		
	/**
	 * 총 결제액
	 */
	function get_payment_history_total_amount($ptype, $user_id, $user_name, $start_dt, $end_dt) {
		log_message('debug',"#### Payment_model::get_payment_history_total_amount($ptype, $user_id, $user_name, $start_dt, $end_dt)");

		$this->db->select_sum('amt','total');
		if (!empty($ptype)) {
			$this->db->where('ptype', $ptype);
		}
		if (!empty($user_id)) {
			$this->db->where('user_id', $user_id);
		}
		if (!empty($user_name)) {
			$this->db->where('ordnm', $user_name);
		}
		if (!empty($start_dt) && !empty($end_dt)) {
			$this->db->where('SUBSTRING(create_dt,1,10) BETWEEN ', " '$start_dt' AND '$end_dt' ", FALSE);
		}
		$query = $this->db->get('tb_payment_history') or $this->logsql->log_sql('error');
		$result = $query->row_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result['total'];
	}
		

}

/* End of file payment_model.php */
/* Location: ./application/models/statistic/payment_model.php */